#! /bin/sh

# rman 执行的命令文件
query=$1
# oracle 数据库名称
db=$2
# 执行 rman 所用的 instance id
sid=$3
# rman 备份数据存放的路径
# oracle 单机备份时，target 为文件系统目录，asm 环境备份时，target 为 diskgrouop 名称
target=$4
# oracle 控制文件和pfile存放路径
store=$5
# rman 日志文件存放路径
rlog=$6

if [ -z "$query" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

if [ -z "$db" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

if [ -z "$sid" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

if [ -z "$target" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

if [ -z "$store" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

if [ -z "$rlog" ];then
   echo "Usage: $0 {<query> <db> <sid> <target> <store> <rlog>}"
   exit 1
fi

os=`uname -s`
awkc=awk
if [ "$os" = "SunOS" ]; then
   awkc=nawk
fi

orauser=${LB_ORACLE_USER}
if [ -z "$orauser" ];then
    orauser=`ps -ef | grep ora_pmon | grep -v "grep" | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$orauser" ];then
    orauser=oracle
fi

user_id=`id ${orauser}`
if [ -z "$user_id" ];then
    echo "can't find user: ${orauser}"
    exit 1
fi

oraclehome=${LB_ORACLE_HOME}
if [ -z "$oraclehome" ]; then
    oraclehome=`cat /etc/oratab | grep ${sid} | grep -v "^#" | head -n1 | cut -d":" -f2`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`ps -ef | grep tns | grep -v grep | grep -i listener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | $awkc '{print $NF}'`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`su - $orauser -c "echo \$ORACLE_HOME"`
fi

listener=${oraclehome}/network/admin

TNS_ADMIN=${listener}

PATH=${oraclehome}/bin:$PATH
ORACLE_HOME=${oraclehome}
ORACLE_SID=${sid}
LD_LIBRARY_PATH=${oraclehome}/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}

export PATH
export ORACLE_SID
export ORACLE_HOME
export TNS_ADMIN
export LD_LIBRARY_PATH

rman=${oraclehome}/bin/rman

#check log file
if [ ! -f "$rlog" ]; then
   touch "$rlog"
fi

grp=""
if [ "$os" = "SunOS" ]; then
    #get group for the user
    grp=`/usr/xpg4/bin/id -gn ${orauser}`
    if [ -n "$grp" ]; then
       chown ${orauser}:${grp} ${rlog}
       chmod 777 ${rlog}
    fi
else
    grp=`id -gn ${orauser}`
    if [ -z "$grp" ]; then
       echo "failed to get group for $grp"
       return 1
    fi
    chown ${orauser}:${grp} ${rlog}
    chmod 777 ${rlog}
fi

chown ${orauser}:${grp} ${store}

NLS_DATE_FORMAT="yyyymmddhh24mi"
NLS_LANG=AMERICAN_AMERICA.US7ASCII
export NLS_DATE_FORMAT
export NLS_LANG

if [ -e "$target" ];then
    if [ ! -e "${target}/datafile" ];then
        mkdir -p ${target}/datafile
        mkdir -p ${target}/archivelog
        mkdir -p ${target}/controlfile
        chown -R ${orauser}:${grp} ${target}
    fi
fi

chmod 777 ${query}
retval=`su - ${orauser} -c "${rman} target / cmdfile=${query} log=${rlog} append"`
status=$?

if [ ${status} -ne 0 ]; then
    echo "Error: $retval"
else
    echo ${retval}
fi

exit ${status}